﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;



namespace Mufasa
{
    public class Mufasa : CombatRoutine
    {
        public override string Name { get { return "Mufasa - Feral & Guardian CC 1.0"; } }
        public override WoWClass Class { get { return WoWClass.Druid; } }
        public static LocalPlayer Me { get { return StyxWoW.Me; } }
        private void slog(string format, params object[] args) { Logging.Write(format, args); }

        public override bool WantButton { get { return true; } }
        public override void Initialize()
        {
                Logging.Write(" -> Mufasa 1.0 <- ");
                Logging.Write(" -> A LazyRaider Feral CC <- ");
        }

        public override void OnButtonPress()
        {    
           Mufasa_Config GUI = new Mufasa_Config(); GUI.ShowDialog();
        }

        #region Buff Method
        private static bool buffMe(string buffName)
        {
            if (!Me.HasAura(buffName))
            {
                CastSpell(buffName);
                return true;
            }
            return false;
        }
        #endregion

        #region Add Dot Method
        private static bool addDot(string dotName)
        {
            if(SpellManager.HasSpell(dotName) && !Me.CurrentTarget.HasAura(dotName) && SpellManager.CanCast(dotName))
            {
                SpellManager.Cast(dotName);
                Logging.Write("-> Casting " + dotName + " on target! <-");
                return true;
            }
            return false;
        }
        #endregion

        #region Cast Spell Method
        public static bool CastSpell(string spellName)
        {
            if (SpellManager.CanCast(spellName))
            {
                if (SpellManager.HasSpell(spellName) && SpellManager.Spells[spellName].CooldownTimeLeft.TotalMilliseconds < 250)
                {
                    SpellManager.Cast(spellName);
                    Logging.Write("-> " + spellName + " <-");
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region Dot Time Left
        private int dotTime(String dotName, WoWUnit unit)
        {
            {
                if (unit.HasAura(dotName))
                {
                    var auras = unit.GetAllAuras();
                    foreach (var a in auras)
                    {
                        if (a.Name == dotName && a.CreatorGuid == Me.Guid)
                        {
                            return a.TimeLeft.Seconds;
                        }
                    }
                }
            }
            return 0;
        }
        #endregion

        #region Refresh Dot Method
        private bool refreshDot(string dotName)
        {
            if (dotTime(dotName, Me.CurrentTarget) < 3)

            {
                SpellManager.Cast(dotName);
                Logging.Write("-> Refreshing " + dotName + " <-");
                return true;
            }
            return false;
        }
        #endregion

        #region LogPoints
        private bool LogPoints()
        {
            var p = Me.ComboPoints;
            if (p == 1) { Logging.Write("-> 1 Combo Point! <-"); return true; }
            if (p == 2) { Logging.Write("-> 2 Combo Points! <-"); return true; }
            if (p == 3) { Logging.Write("-> 3 Combo Points! <-"); return true; }
            if (p == 4) { Logging.Write("-> 4 Combo Points! <-"); return true; }
            if (p == 5) { Logging.Write("-> 5 Combo Points! <-"); return true; }
            return false;
        }
        #endregion

        #region Combat Rotation
        public override void Combat()
        {
            if (Me.CurrentEnergy > 40 && Me.IsSafelyBehind(Me.CurrentTarget))
            {
                CastSpell("Shred");
            }

            if (Me.CurrentEnergy > 35 && Me.IsSafelyFacing(Me.CurrentTarget))
            {
                CastSpell("Mangle");
            }
            
            if (Me.ComboPoints == 5 && Me.CurrentTarget.HasAura("Rake") && !Me.CurrentTarget.HasAura("Rip") && Me.CurrentEnergy > 30)
            { 
                addDot("Rip");
            }

            if (Me.ComboPoints < 5 && !Me.CurrentTarget.HasAura("Rake") && Me.CurrentEnergy > 35)
            {
                addDot("Rake");
            }

            if (!Me.HasAura("Savage Roar") && Me.ComboPoints == 5 && Me.CurrentTarget.HasAura("Rake") && Me.CurrentTarget.HasAura("Rip") && Me.CurrentEnergy > 25)
            {
                CastSpell("Savage Roar");
            }
            if (Me.ComboPoints == 5 && Me.CurrentEnergy > 30 && Me.CurrentTarget.HealthPercent < 45 )
            {
                if(Mufasa_Settings.Instance.Tier13 == true)
                {
                    CastSpell("Ferocious Bite");
                }
            }
            if (Me.ComboPoints == 5 && Me.CurrentEnergy > 30 )
            {
                if (Mufasa_Settings.Instance.Tier13 == false)
                {
                    refreshDot("Rip");
                }
            }

            if (Me.ComboPoints < 5 && Me.CurrentEnergy > 35)
            {
                refreshDot("Rake");
            }

            if (Me.ComboPoints == 5 && Me.CurrentEnergy > 25 && dotTime("Savage Roar", Me) < 8)
            {
                CastSpell("Savage Roar");
            }

            if (Me.CurrentEnergy < 30 && Me.CurrentTarget.HasAura("Rake") && Me.CurrentTarget.HasAura("Rip"))
            {
                CastSpell("Tiger's Fury");
            }

            if (Me.HasAura("Tiger's Fury") && Me.CurrentEnergy > 75)
            {
                CastSpell("Berserk");
            }
   

        }
        #endregion

    }

}
